import java.util.LinkedList;
import java.util.Queue;

class Solution {
    public int[] dx = new int[] {0, 0, 1, -1};
    public int[] dy = new int[] {1, -1, 0, 0};
    public boolean[][] vis = new boolean[201][201];
    public void solve(char[][] board) {
        int m = board.length;
        int n = board[0].length;
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                if(board[i][j] == 'O' && !vis[i][j]){
                    if(!find(board, i, j)){
                        replace(board, i, j);
                    }
                }
            }
        }
    }
    public boolean find(char[][] board, int i, int j){
        int m = board.length;
        int n = board[0].length;
        Queue<int[]> queue = new LinkedList<>();
        boolean flag = false;
        queue.offer(new int[] {i, j});
        vis[i][j] = true;
        if(i == 0 || j == 0 || i == m - 1 || j == n - 1){
            flag = true;
        }
        while(!queue.isEmpty()){
            int[] point = queue.poll();
            int a = point[0];
            int b = point[1];
            for(int k = 0; k < 4; k++){
                int x = a + dx[k];
                int y = b + dy[k];
                if(x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'O' && !vis[x][y]){
                    if(x == 0 || y == 0 || x == m - 1 || y == n - 1){
                        flag = true;
                    }
                    queue.offer(new int[] {x, y});
                    vis[x][y] = true;
                }
            }
        }
        return flag;
    }
    public void replace(char[][] board, int i, int j){
        int m = board.length;
        int n = board[0].length;
        Queue<int[]> queue = new LinkedList<>();
        queue.offer(new int[] {i, j});
        board[i][j] = 'X';
        while(!queue.isEmpty()){
            int[] point = queue.poll();
            int a = point[0];
            int b = point[1];
            for(int k = 0; k < 4; k++){
                int x = a + dx[k];
                int y = b + dy[k];
                if(x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'O'){
                    queue.offer(new int[] {x, y});
                    board[x][y] = 'X';
                }
            }
        }
    }
}